<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/sankun.png" alt />
        <span>商品后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width="collapseFlag?'64px':'200px'">
        <div class="toggle" @click="toggleCollaspe">|||</div>
        <el-menu
          :default-active="active"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="skyblue"
          unique-opened
          :collapse="collapseFlag"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menueList" :key="item.id">
            <!-- 一级菜单标题和内容 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="setActive('/'+item2.path)"  :index="'/'+item2.path" v-for="item2 in item.children" :key="item2.id">
              <!-- 二级菜单标题和内容 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menueList: [],
      iconObj: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      collapseFlag: false,
      active:''
    };
  },

  methods: {
    // 退出
    logout() {
      this.$router.push("/login");
      sessionStorage.removeItem("token");
      // sessionStorage.clear();
      this.$mg.success("亲!退出成功哦!");
    },
    // 获取菜单列表数据
    async getMenues() {
      const res = await this.axios.get("menus");
      // console.log(res);
      if (res.meta.status == 200) {
        this.menueList = res.data;
      }
      // console.log(this.menueList);
    },
    // 点击折叠侧边栏
    toggleCollaspe() {
      this.collapseFlag = !this.collapseFlag;
    },
    // 设置当前标题高亮显示
    setActive(index){
      // console.log(index);
      this.active=index;
      sessionStorage.setItem('activeIndex',index);
    }
  },

  // 生成dom结构之前调用
  created() {
    this.getMenues();
    var activeIndex=sessionStorage.getItem('activeIndex');
    // console.log(activeIndex);
    this.active=activeIndex;
  }
};
</script>

<style lang='less' scoped>
.home_container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 10px 20px;
  div {
    display: flex;
    align-items: center;
    span {
      color: #fff;
      font-size: 20px;
      margin-left: 15px;
    }
  }
}
.el-aside {
  background: #333744;
  .toggle {
    height: 30px;
    line-height: 30px;
    background-color: skyblue;
    text-align: center;
    user-select: none;
    cursor: pointer;
    color: #fff;
  }
}
.el-main {
  background: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
</style>
